package com.hhh.demo.sx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhh.demo.sx.bean.Product;
import com.hhh.demo.sx.bean.Record;
import com.hhh.demo.sx.bean.Store;
import com.hhh.demo.sx.exception.MyException;
import com.hhh.demo.sx.mapper.StoreMapper;
import com.hhh.demo.sx.service.ProductService;
import com.hhh.demo.sx.service.StoreService;
import com.hhh.demo.sx.util.ResultMap;
import com.hhh.demo.sx.util.ResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhh
 * @since 2022-01-04
 */
@Controller
@RequestMapping("/store")
public class StoreController {


    @Autowired
    private StoreService storeService;
    /**
     * 打开商品主页面
     */
    @RequestMapping("/index")
    public String openPage() {
        return "store";
    }

    /**
     * 查询仓库列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> getList(@RequestParam("page") int p,
                                       @RequestParam("limit") int limit,
                                       @RequestParam(value = "condition", required = false) String condition) {

        return storeService.findAll(p, limit, condition);

    }
    /**
     * 查询仓库列表
     */
    @ResponseBody
    @RequestMapping("/list1")
    public ResultMap getList1() {

        return storeService.findByUser();

    }
    /**
     * 新增/修改仓库
     */
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public ResultMap saveOrUpdate(Store store) {
        System.out.println(store);
        QueryWrapper<Store> wrapper = new QueryWrapper<>();
        wrapper.eq("name", store.getName());
        if (storeService.getOne(wrapper) != null&&store.getId()==null) {
            throw new MyException("该仓库已存在");

        }
        boolean f = storeService.saveOrUpdate(store);
        if (f) {
            return ResultMap.success();
        } else {
            return ResultMap.fail();
        }
    }

    /**
     * 查找仓库
     */
    @ResponseBody
    @RequestMapping("/getOne")
    public ResultMap getOne(int id) {
        Store store = storeService.getById(id);
        return ResultMap.success(store);
    }

    /**
     * 删除仓库
     */
    @ResponseBody
    @RequestMapping("/delete")
    public ResultMap delete(int id) {
        return storeService.removeById(id)?ResultMap.success():ResultMap.fail();
    }


}

